package com.yikaiye.android.yikaiye.wxapi;

import android.content.Intent;
import android.util.Log;

import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.yikaiye.android.yikaiye.R;
import com.yikaiye.android.yikaiye.base.BaseActivity;
import com.yikaiye.android.yikaiye.base.BaseViewModel;
import com.yikaiye.android.yikaiye.config.Constant;
import com.yikaiye.android.yikaiye.databinding.ActivityWXPayEntryBinding;
import com.yikaiye.android.yikaiye.ui.activity.MainActivity;
import com.yikaiye.android.yikaiye.utils.JumpIntent;
import com.yikaiye.android.yikaiye.utils.UMengUtil;

public class WXPayEntryActivity extends BaseActivity<ActivityWXPayEntryBinding, BaseViewModel> implements IWXAPIEventHandler {

    private IWXAPI api;
    private boolean isPaySuccess = false;

    @Override
    protected BaseViewModel bindModel() {
        return getViewModel(this, BaseViewModel.class);
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    public void initData() {
        initTitleBar("支付结果");
        api = WXAPIFactory.createWXAPI(this, Constant.WE_CHAT_APP_ID);
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq baseReq) {

    }

    @Override
    public void onResp(BaseResp baseResp) {
        /**
         * 0 成功
         * -1 错误，可能的原因：签名错误、未注册APPID、项目设置APPID不正确、注册的APPID与设置的不匹配、其他异常等。
         * -2 取消
         */
        Log.e("payCode", baseResp.errCode + "");
        if (baseResp.errCode == 0) {
            isPaySuccess = true;
            binding.ivImage.setImageResource(R.mipmap.icon_pay_yes);
            binding.tvPayStatus.setText(getString(R.string.pay_success));
            binding.tvLookOrder.setText(getString(R.string.look_order));
//            EventBus.getDefault().post(new EventBusModel("refresh_order"));
            UMengUtil.setCountEventId(Constant.PAGE_PAY_SUCCESS);
        } else {
            isPaySuccess = false;
            binding.ivImage.setImageResource(R.mipmap.icon_pay_no);
            binding.tvPayStatus.setText(getString(R.string.pay_fail));
            binding.tvLookOrder.setText(getString(R.string.pay_again));
            UMengUtil.setCountEventId(Constant.PAGE_PAY_FAIL);
        }
    }

    @Override
    protected void initClick() {
        viewModel.onDelayClick(binding.tvLookOrder, o -> {
            Constant.isFromPay = true;
            if (isPaySuccess) {
                Constant.currentPosition = 2;
                UMengUtil.setCountEventId(Constant.PAGE_LOOK_ORDER);
            } else {
                Constant.currentPosition = 1;
                UMengUtil.setCountEventId(Constant.PAGE_PAY_AGAIN);
            }
            JumpIntent.jump(WXPayEntryActivity.this, MainActivity.class, true);
        });
    }
}